package com.qjc.mq.consumer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 * @ClassName: Consumer
 * @Description: 消息消费者
 * @Author: qjc
 * @Date: 2021/12/13 12:00 下午
 */
@Component
/**
 * 设置消息监听，注解属性说明
 * 1.监听组(consumerGroup)：监听topic(topic)：监听tag(selectorExpression)(默认监听topic下所有)
 * 2.监听消费模式(messageModel):默认负载均衡（即集群模式）：CLUSTERING（每一个消息只发给一个消费者）、广播模式：BROADCASTING（发送给所有消费者）
 *      a) 广播模式下，不管是不是同一个消费者组的消费者，每个消费者都消费相同的消息;
 *      b) 集群模式（默认模式）下，同一消费者组的消费者消费不同的消息（相当于RabbitMQ的work模式），不同消费者组的消费者消费相同的消息（相当于RabbitMQ的发布/订阅模式）。
 * 3.设置顺序消息处理模式(consumeMode)（默认是所有线程可以处理同一个消息队列（ConsumeMode.CONCURRENTLY）,当前消息没有线程在执行时其他线程才能够执行（ConsumeMode.ORDERLY）。
 *   ps:一个线程顺序执行一个队列表时消息监听必须使用负载均衡messageModel = MessageModel.BROADCASTING）
 * 4.设置过滤tags类型：默认时tag（SelectorType.TAG）,可以修改为sql语法(SelectorType.SQL92)：
 */
@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group}", topic = "${rocketmq.consumer.topic}",
        messageModel = MessageModel.CLUSTERING)
// 同步消息消费者监听
//@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group}", topic = "${rocketmq.consumer.topic}",
//        selectorExpression = "${rocketmq.consumer.sync-tag}", messageModel = MessageModel.CLUSTERING)
// 异步消息消费者监听
//@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group}", topic = "${rocketmq.consumer.topic}",
//        selectorExpression = "${rocketmq.consumer.async-tag}", messageModel = MessageModel.CLUSTERING)
// 单向消息消费者监听
//@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group}", topic = "${rocketmq.consumer.topic}",
//        selectorExpression = "${rocketmq.consumer.oneway-tag}", messageModel = MessageModel.CLUSTERING)
// 顺序消息消费者监听
//@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group}", topic = "${rocketmq.consumer.topic}",
//        selectorExpression = "${rocketmq.consumer.sequence-tag}", messageModel = MessageModel.CLUSTERING,
//        consumeMode = ConsumeMode.ORDERLY)
// 延迟消息消费者监听
//@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group}", topic = "${rocketmq.consumer.topic}",
//        selectorExpression = "${rocketmq.consumer.delay-tag}", messageModel = MessageModel.CLUSTERING)
// 批量消息消费者监听
//@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group}", topic = "${rocketmq.consumer.topic}",
//        selectorExpression = "${rocketmq.consumer.batch-tag}", messageModel = MessageModel.CLUSTERING)
// SQL92过滤消息消费者监听
// 使用相同的消费者组，广播模式下，消费的消息相同，且sql过滤条件一样；集群模式下，消费的消息不同，但是sql过滤条件一样
// 使用不同的消费者组，集群模式/广播模式下，每个消费者都会根据过滤条件过滤出想要的数据
//@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group-second}", topic = "${rocketmq.consumer.topic}",
//        selectorExpression = "${rocketmq.consumer.sql92-tag}", messageModel = MessageModel.CLUSTERING,
//        selectorType = SelectorType.SQL92)
// 事务消息消费者监听
//@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group}", topic = "${rocketmq.consumer.topic}",
//        selectorExpression = "${rocketmq.consumer.transaction-tag}", messageModel = MessageModel.CLUSTERING)
@Slf4j
public class Consumer implements RocketMQListener<String> {

    @Override
    public void onMessage(String message) {
        System.out.println("2-消费消息：【" + message + "】");
    }


}